package services

import (
	"gorm.io/gorm"
	"mygo/app/models"
	"mygo/app/tools"
	"strconv"
	"strings"
)

type MergedData struct {
	Date      string
	Time      string
	WaveValue string
	WaveAngle string
	WindValue string
	WindAngle string
}

type MaxData struct {
	WaveValue string
	WindValue string
}

func HandelXhl(forecast *models.Forecast, time string, db *gorm.DB) {
	timeArr := strings.Split(time, "-")
	year := timeArr[0]
	month := timeArr[1]
	day := timeArr[2]
	for i, report := range forecast.Reports {
		if i == 0 {
			for j := 1; j < 4; j++ {
				y, m, d := tools.GetNextDay(year, month, day, j)
				if len(m) == 1 {
					m = "0" + m
				}
				if len(d) == 1 {
					d = "0" + d
				}
				var xhlData []models.XhlData
				timeNew := y + "-" + m + "-" + d
				report.Data[0][j+1].Value = m + "月" + d + "日"
				//timeNew = "2024-07-11"

				//var results []MergedData

				var xhlDataTemp []models.XhlData
				if db.Where("date = ? AND file = ?", timeNew, "xhl02").Find(&xhlDataTemp).RowsAffected == 0 {
					continue
				}

				// 查询 xhl01, xhl02, xhl03 三种文件的数据
				db.Table("xhl_data").
					Select("date, time,MAX(CAST(waveValue AS DECIMAL(10,2))) as waveValue, MAX(waveAngle) as waveAngle, MAX(CAST(windValue AS DECIMAL(10,2))) as windValue, MAX(windAngle) as windAngle").
					Where("date = ?", timeNew).
					Group("date, time").
					Find(&xhlData)

				// 针对 xhl02 的数据，更新 WaveAngle 和 WindAngle
				for idx, v := range xhlDataTemp {
					if v.File == "xhl02" {
						xhlData[idx].WaveAngle = v.WaveAngle
						xhlData[idx].WindAngle = v.WindAngle
					}
				}

				isContinue := false
				for _, v := range xhlData {
					if v.WaveValue == "" || v.WaveAngle == "" || v.WindValue == "" || v.WindAngle == "" {
						isContinue = true
						break
					}
				}
				if isContinue {
					continue
				}

				var waveValueArr []float64
				//var waveAngleArr []string
				var windValueArr []float64
				//var windAngleArr []string

				var waveAngleArrTest []float64
				var windAngleArrTest []float64
				for _, v := range xhlData {
					waveValue, _ := strconv.ParseFloat(v.WaveValue, 64)
					waveAngle, _ := strconv.ParseFloat(v.WaveAngle, 64)
					windValue, _ := strconv.ParseFloat(v.WindValue, 64)
					windAngle, _ := strconv.ParseFloat(v.WindAngle, 64)
					waveValueArr = append(waveValueArr, waveValue)
					windValueArr = append(windValueArr, windValue)
					//waveAngleArr = append(waveAngleArr, waveAngle)
					windAngleArrTest = append(windAngleArrTest, windAngle)
					waveAngleArrTest = append(waveAngleArrTest, waveAngle)

					//for k, vRange := range angleMap {
					//	if v.WaveAngle != "" {
					//		if waveAngle >= vRange[0] && waveAngle < vRange[1] {
					//			waveAngleArr = append(waveAngleArr, k)
					//			break
					//		}
					//		if k == "N'" {
					//			if (waveAngle >= vRange[0] && waveAngle < 360) || (waveAngle >= 0 && waveAngle < vRange[1]) {
					//				waveAngleArr = append(waveAngleArr, k)
					//				break
					//			}
					//		}
					//	}
					//}
					//for k, vRange := range angleMap {
					//	if v.WindAngle != "" {
					//		if windAngle >= vRange[0] && windAngle < vRange[1] {
					//			windAngleArr = append(windAngleArr, k)
					//			break
					//		}
					//		if k == "N'" {
					//			if (windAngle >= vRange[0] && windAngle < 360) || (windAngle >= 0 && windAngle < vRange[1]) {
					//				windAngleArr = append(windAngleArr, k)
					//				break
					//			}
					//		}
					//	}
					//}
				}

				maxValue, minValue, maxIdx, minIdx := tools.MaxAndMinAndIndex(waveValueArr)
				maxValue = tools.Float64ToFixed(maxValue, 1)
				minValue = tools.Float64ToFixed(minValue, 1)
				if maxValue-minValue > 0.3 {
					if minIdx < maxIdx {
						bw := getDayTime(tools.IntToStr(maxIdx%24) + ":00")
						report.Data[3][j+1].Value = tools.Float64ToFixedStr(minValue, 1) + "，" + bw + "增大到" + tools.Float64ToFixedStr(maxValue, 1)

						//report.Data[1][j].Value = tools.Float64ToFixedStr(minValue, 1) + "-" + tools.Float64ToFixedStr(maxValue, 1) + "，" + bw + "增大到" + tools.Float64ToFixedStr(maxValue, 1)
					} else {
						bw := getDayTime(tools.IntToStr(minIdx%24) + ":00")
						report.Data[3][j+1].Value = tools.Float64ToFixedStr(maxValue, 1) + "，" + bw + "减小到" + tools.Float64ToFixedStr(minValue, 1)

						//report.Data[1][j].Value = tools.Float64ToFixedStr(maxValue, 1) + "-" + tools.Float64ToFixedStr(minValue, 1) + "，" + bw + "减小到" + tools.Float64ToFixedStr(minValue, 1)
					}
				} else {
					report.Data[3][j+1].Value = tools.Float64ToFixedStr(minValue, 1) + "-" + tools.Float64ToFixedStr(maxValue, 1)
				}

				maxValueWind, minValueWind, maxIdxWind, minIdxWind := tools.MaxAndMinAndIndex(windValueArr)
				maxValueWind = tools.Float64ToFixed(maxValueWind, 1)
				minValueWind = tools.Float64ToFixed(minValueWind, 1)

				windLevel1, windLevel2 := "", ""
				for k, vRange := range windMap {
					if maxValueWind >= vRange[0] && maxValueWind <= vRange[1] {
						windLevel1 = k
					}
					if minValueWind >= vRange[0] && minValueWind <= vRange[1] {
						windLevel2 = k
					}
				}
				if windLevel1 == windLevel2 {
					report.Data[1][j+1].Value = windLevel1 + "▽"
				} else {
					if minIdxWind < maxIdxWind {
						bw := getDayTime(tools.IntToStr(maxIdxWind%24) + ":00")
						report.Data[1][j+1].Value = windLevel2 + "▽，" + bw + "增大到" + windLevel1 + "▽"
					} else {
						bw := getDayTime(tools.IntToStr(minIdxWind%24) + ":00")
						report.Data[1][j+1].Value = windLevel1 + "▽，" + bw + "减小到" + windLevel2 + "▽"
					}
				}

				m1, m2, _, _ := tools.GetMaxAngleDiff(windAngleArrTest)
				mA1 := tools.GetAngleDirection(m1)
				mA2 := tools.GetAngleDirection(m2)

				w1, w2, _, _ := tools.GetMaxAngleDiff(waveAngleArrTest)
				wA1 := tools.GetAngleDirection(w1)
				wA2 := tools.GetAngleDirection(w2)

				if mA1 == mA2 {
					report.Data[2][j+1].Value = mA1
				} else {
					report.Data[2][j+1].Value = mA1 + "转" + mA2
				}

				if wA1 == wA2 {
					report.Data[4][j+1].Value = wA1
				} else {
					report.Data[4][j+1].Value = wA1 + "转" + wA2
				}

				//if waveAngleArr[0] == waveAngleArr[len(waveAngleArr)-1] {
				//	report.Data[4][j+1].Value = waveAngleArr[0]
				//} else {
				//	report.Data[4][j+1].Value = waveAngleArr[0] + "转" + waveAngleArr[len(waveAngleArr)-1]
				//}
				//
				//if windAngleArr[0] == windAngleArr[len(windAngleArr)-1] {
				//	report.Data[2][j+1].Value = windAngleArr[0]
				//} else {
				//	report.Data[2][j+1].Value = windAngleArr[0] + "转" + windAngleArr[len(windAngleArr)-1]
				//}

			}
		} else if i == 1 {
			y, m, d := tools.GetNextDay(year, month, day, 1)
			if len(m) == 1 {
				m = "0" + m
			}
			if len(d) == 1 {
				d = "0" + d
			}
			timeOld := year + "-" + month + "-" + day
			timeNew := y + "-" + m + "-" + d

			//timeOld = "2024-07-11"
			//timeNew = "2024-07-12"
			report.Data[0][2].Value = month + "月" + day + "日夜间-" + m + "月" + d + "日凌晨"
			report.Data[0][6].Value = m + "月" + d + "日白天"

			skArr := [][]string{{timeOld + " 17:00", timeOld + " 18:00", timeOld + " 19:00", timeOld + " 20:00", timeOld + " 21:00"},
				{timeOld + " 21:00", timeOld + " 22:00", timeOld + " 23:00", timeNew + " 00:00", timeNew + " 01:00"},
				{timeNew + " 01:00", timeNew + " 02:00", timeNew + " 03:00", timeNew + " 04:00", timeNew + " 05:00"},
				{timeNew + " 05:00", timeNew + " 06:00", timeNew + " 07:00", timeNew + " 08:00", timeNew + " 09:00"},
				{timeNew + " 09:00", timeNew + " 10:00", timeNew + " 11:00", timeNew + " 12:00", timeNew + " 13:00"},
				{timeNew + " 13:00", timeNew + " 14:00", timeNew + " 15:00", timeNew + " 16:00", timeNew + " 17:00"}}
			for idx, v := range skArr {
				//var xhlData []models.XhlData
				//获取5个时刻的最大风力和波高
				var xhlDataTemp []models.XhlData
				var xhlDataTemp2 []models.XhlData

				for _, vTime := range v {
					var xhlData []models.XhlData
					db.Table("xhl_data").
						Where("date = ? AND time = ?", vTime[0:10], vTime[11:16]).
						Find(&xhlData)
					xhlDataTemp = append(xhlDataTemp, xhlData...)

					var xhlData2 []models.XhlData
					db.Table("xhl_data").
						Where("file = ?", "xhl02").
						Where("date = ? AND time = ?", vTime[0:10], vTime[11:16]).
						Find(&xhlData2)
					xhlDataTemp2 = append(xhlDataTemp2, xhlData2...)
				}

				if len(xhlDataTemp2) != 5 {
					continue
				}

				//	获取5个时刻的最大风力和波高
				maxWindValue, maxWaveValue := getMaxWindAndWave(xhlDataTemp)

				for k, vRange := range windMap {
					if maxWindValue >= vRange[0] && maxWindValue < vRange[1] {
						report.Data[2][idx+2].Value = k + "▽"
						break
					}
				}
				report.Data[4][idx+2].Value = tools.Float64ToFixedStr(maxWaveValue, 1)

				for k, vRange := range angleMap {
					if xhlDataTemp2[2].WaveAngle != "" {
						waveAngle, _ := strconv.ParseFloat(xhlDataTemp2[2].WaveAngle, 64)
						if waveAngle >= vRange[0] && waveAngle < vRange[1] {
							report.Data[5][idx+2].Value = k
							break
						}
						if k == "N'" {
							if (waveAngle >= vRange[0] && waveAngle < 360) || (waveAngle >= 0 && waveAngle < vRange[1]) {
								report.Data[5][idx+2].Value = k
								break
							}
						}
					}
				}

				for k, vRange := range angleMap {
					if xhlDataTemp2[2].WindAngle != "" {
						windAngle, _ := strconv.ParseFloat(xhlDataTemp2[2].WindAngle, 64)
						if windAngle >= vRange[0] && windAngle < vRange[1] {
							report.Data[3][idx+2].Value = k
							break
						}
						if k == "N'" {
							if (windAngle >= vRange[0] && windAngle < 360) || (windAngle >= 0 && windAngle < vRange[1]) {
								report.Data[3][idx+2].Value = k
								break
							}
						}
					}
				}

			}

		}

	}
}

// getMaxWindAndWave
func getMaxWindAndWave(xhlDataTemp []models.XhlData) (maxWindValue float64, maxWaveValue float64) {
	var waveValueArr []float64
	var windValueArr []float64
	for _, v := range xhlDataTemp {
		waveValue, _ := strconv.ParseFloat(v.WaveValue, 64)
		windValue, _ := strconv.ParseFloat(v.WindValue, 64)
		waveValueArr = append(waveValueArr, waveValue)
		windValueArr = append(windValueArr, windValue)
	}
	maxWindValue, _, _, _ = tools.MaxAndMinAndIndex(windValueArr)
	maxWaveValue, _, _, _ = tools.MaxAndMinAndIndex(waveValueArr)
	return maxWindValue, maxWaveValue
}
